#ifndef RADIOMETER_H
#define RADIOMETER_H

#include <QObject>
#include <QSerialPort>
#include <QDateTime>

// extern const int RADIO_NSUPROTOCOL_LEN;
// extern const int RADIO_STAPROTOCOL_LEN;
// extern const int RADIO_SETDATETIMEPROTOCOL_LEN;

//太阳辐射计传回的不同数据包长度
#define RADIO_NSUPROTOCOL_LEN 160
#define RADIO_STAPROTOCOL_LEN 89 //这个不定长？
#define RADIO_AODPROTOCOL_LEN 89
#define RADIO_SETDTPROTOCOL_LEN 26
#define RADIO_DATEPROTOCOL_LEN 15
#define RADIO_TIMEPROTOCOL_LEN 13
#define RADIO_LATPROTOCOL_LEN 9
#define RADIO_LONPROTOCOL_LEN 11
#define RADIO_ALTIPROTOCOL_LEN 10
#define RADIO_AZPROTOCOL_LEN 9
#define RADIO_EIPROTOCOL_LEN 8
#define RADIO_RAINPROTOCOL_LEN 6

//STA协议对应结构体
class RadioMeterSTAProtocolAll{
public:
    QDateTime dateTime;
    QString radioMeterStatus;
    double temperature;
    double humidity;
    double voltage;
    QString errCode;
    double heightAngle;
    double azimuthAngle;
    QString turnTableStatus;
    QString powerStatus;

};
//NSU协议对应结构体
class RadioMeterNSUProtocolAll{
public:
    QDateTime dateTime;
    double sun1020;
    double sunI1640;
    double sun870;
    double sun675;
    double sun440;
    double sun500;
    double sunI1020;
    double sun937;
    double sun380;
    double sun340;
    double headTemperature;
    double ambientTemperature;
    double airPressure;


};
//AOD协议对应结构体
class RadioMeterAODProtocolAll{
public:
    QDateTime dateTime;
    double sun1020;
    double sunI1640;
    double sun870;
    double sun675;
    double sun440;
    double sun500;
    double sunI1020;
    double sun937;
    double sun380;
    double sun340;
    double PWV;
};

//太阳辐射计解析类
class RadioMeter:public QObject
{
    Q_OBJECT
public:
    QSerialPort* m_port;
    QByteArray m_readBuffer;
    QMap<QByteArray, std::function<void(const QByteArray&)>> m_parseMap;

    static RadioMeter* getInstance();
    bool openDevice(QString portName);
    void closeDevice();

    ~RadioMeter();


signals:
    void updateView_NSU(const RadioMeterNSUProtocolAll& protocol);
    void updateView_STA(const RadioMeterSTAProtocolAll& protocol);
    void updateView_AOD(const RadioMeterAODProtocolAll& protocol);
    void updateView_LAT(const QString& lat);
    void updateView_LON(const QString& lon);

    void protocolNSU(const QByteArray& ary);
    void protocolSTA(const QByteArray& ary);
    void protocolAOD(const QByteArray& ary);

private:
    static RadioMeter* instance;
    RadioMeter(QObject* parent = nullptr);

    //不同的数据头对应的解析函数，
    void parseNSU(const QByteArray& ary);
    void parseSTA(const QByteArray& ary);
    void parseAOD(const QByteArray &ary);
    //void parseSETDATETIME(const QByteArray& ary);
    //void parseDATE(const QByteArray& ary);
    //void parseTIME(const QByteArray& ary);
    void parseLAT(const QByteArray& ary);
    void parseLON(const QByteArray& ary);
    //void parseALTI(const QByteArray& ary);
    void parseAZ(const QByteArray& ary);
    void parseEI(const QByteArray& ary);
    //void parseRAIN(const QByteArray& ary);
    void parseSYSTEMRESET(const QByteArray& ary);
    void parseSETTIMESPAN(const QByteArray &);
};

#endif // RADIOMETER_H
